/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.entityGenerator.base.dataTypeData.nestedConfigs;

import com.inspur.edp.caf.generator.BaseGenerator;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.item.ClassGenerator;
import com.inspur.edp.caf.generator.item.ClassGeneratorContext;
import com.inspur.edp.cef.designtime.api.IGspCommonDataType;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.common.MetadataAdapter;
import com.inspur.edp.cef.generator.entityGenerator.base.dataTypeData.DataTypeDataGeneratorContext;
import com.inspur.edp.cef.spi.entity.INestedConfig;
import com.inspur.edp.udt.designtime.api.entity.ComplexDataTypeDef;
import com.inspur.edp.udt.designtime.api.entity.SimpleDataTypeDef;
import com.inspur.edp.udt.designtime.api.entity.UnifiedDataTypeDef;
import lombok.var;
import org.eclipse.jdt.core.dom.Modifier;

import java.util.ArrayList;
import java.util.Arrays;

public class NestedObjectConfigGenerator extends ClassGenerator {
    private IGspCommonDataType dataType;
    private IGspCommonField field;
    private DataTypeDataGeneratorContext dataGenContext;
    private UnifiedDataTypeDef udt;

    public NestedObjectConfigGenerator(
            IGspCommonDataType dataType,
            IGspCommonField field,
            DataTypeDataGeneratorContext dataGenContext) {
        super(dataGenContext.getDataTypeGeneratorContext().getEntityGenContext().getCompilationUnitInfo());
        this.dataGenContext = dataGenContext;
        this.dataType = dataType;
        this.field = field;

        //        udt =
//                (UnifiedDataTypeDef)
//                        ((MetadataAdapter.getMetadata(field.getUdtID()).getContent() instanceof UnifiedDataTypeDef)
//                                ? MetadataAdapter.getMetadata(field.getUdtID()).getContent()
//                                : null);
        udt=(UnifiedDataTypeDef)(
                (dataGenContext.getDataTypeGeneratorContext().getEntityGenContext().
                getCefGenContext().getMetadataJitContext().getMetadata(field.getUdtID()).getContent() instanceof  UnifiedDataTypeDef)
                        ? dataGenContext.getDataTypeGeneratorContext().
                        getEntityGenContext().getCefGenContext().
                        getMetadataJitContext().
                        getMetadata(field.getUdtID()).getContent()
                        :null
        );


        dataGenContext
                .getNestedConfigTypes()
                .put(field.getLabelID(), new MediateType(dataGenContext.getDataTypeGeneratorContext().getEntityGenContext().getDefaultPackageName(), getName()));
    }

    @Override
    protected ClassGeneratorContext createClassInfoContext() {
        return new ClassGeneratorContext();
    }

    @Override
    protected ArrayList<Modifier.ModifierKeyword> getAccessModifier() {
        return new ArrayList(
                Arrays.asList(new Modifier.ModifierKeyword[]{Modifier.ModifierKeyword.PUBLIC_KEYWORD}));
    }

    @Override
    protected String getName() {
        return getName(dataType, field);
    }

    public static String getName(IGspCommonDataType dataType, IGspCommonField field) {
        return String.format("%1$s%2$sNestedConfigs", dataType.getCode(), field.getLabelID());
    }

    @Override
    protected ArrayList<TypeInfo> addParentInterfaces() {

        ArrayList<TypeInfo> list = super.addParentInterfaces();
        if (list == null) {
            list = new ArrayList<TypeInfo>();
        }
        TypeInfo typeInfo = new TypeInfo("INestedConfig");
        typeInfo.setTypePackageName("com.inspur.edp.cef.spi.entity");

        list.add(typeInfo);
        return list;
    }

    @Override
    protected ArrayList<BaseGenerator> createClassExtendChildGenrators() {

        ArrayList<BaseGenerator> list = super.createClassExtendChildGenrators();
        if (list == null) {
            list = new java.util.ArrayList<BaseGenerator>();
        }
        list.add(new NestedConfigGetConfigIdGenerator(udt.getUdtType()));
        if (udt instanceof SimpleDataTypeDef) {
            list.add(
                    new SimpleUdtMappingGenerator(
                            dataType,
                            field,
                            (SimpleDataTypeDef) ((udt instanceof SimpleDataTypeDef) ? udt : null),
                            dataGenContext));
        } else if (udt instanceof ComplexDataTypeDef) {
            list.add(
                    new ComplexUdtMappingGenerator(
                            dataType,
                            field,
                            (ComplexDataTypeDef) ((udt instanceof ComplexDataTypeDef) ? udt : null),
                            dataGenContext));
        }
        return list;
    }
}
